package com.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dao.RecruitDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.RecruitCountVO;
import com.po.RecruitInfoPO;

@Controller
public class RecruitAction implements ServletRequestAware {
	private HttpServletRequest request;
	@Autowired
	private RecruitDao recruitDao;

	private RecruitCountVO recruitCountVO;
	private List<RecruitInfoPO> listRecruit;
	private List<Integer> listPage = new ArrayList<Integer>();

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	/*
	 * 查询最新的招聘信息
	 */

	public String showInfo() {
		/*
		 * 1.查询部分分页数据
		 */
		int page = Integer.parseInt(request.getParameter("page"));
		int pn = Integer.parseInt(request.getParameter("pn"));
		Map<String, Object> map = new HashMap<String, Object>();
		int num = page * pn;
		map.put("num", num);
		map.put("pn", pn);
		listRecruit = recruitDao.findRecruitInfo(map);

		/*
		 * 2.查询数据库中全部数据的数量，并计算需要分页的页数时
		 */

		recruitCountVO = recruitDao.findRecruitCount();
		int count = recruitCountVO.getCount();
		int countPage = 0;

		if (count % pn == 0) {
			countPage = count / pn;
		} else {
			countPage = count / pn + 1;
		}

		/*
		 * 3.获取分页的页数信息
		 */

		if (countPage >= 5) {
			listPage.add(countPage - 2);
			listPage.add(countPage - 1);
			listPage.add(countPage);
			listPage.add(countPage + 1);
			listPage.add(countPage + 2);

		} else {
			for (int i = 1; i <= countPage; i++) {
				listPage.add(i);
			}
		}
		// 获取真实的当前页数
		int currentPage = page + 1;

		ActionContext.getContext().put("listRecruit", listRecruit);
		ActionContext.getContext().put("countPage", countPage);
		ActionContext.getContext().put("listPage", listPage);
		ActionContext.getContext().put("currentPage", currentPage);

		return "show";
	}
	
	
	
	
	public RecruitCountVO getRecruitCountVO() {
		return recruitCountVO;
	}

	public void setRecruitCountVO(RecruitCountVO recruitCountVO) {
		this.recruitCountVO = recruitCountVO;
	}

	public List<Integer> getListPage() {
		return listPage;
	}

	public void setListPage(List<Integer> listPage) {
		this.listPage = listPage;
	}

	public List<RecruitInfoPO> getListRecruit() {
		return listRecruit;
	}

	public void setListRecruit(List<RecruitInfoPO> listRecruit) {
		this.listRecruit = listRecruit;
	}

}
